var WebSocketServer = require('ws').Server;
var https = require('https');
var fs = require('fs');
var tlsOptions = {
    key: fs.readFileSync(__dirname +  '/../ssl/2_ws.tdreamer.cc.key'),
    cert: fs.readFileSync(__dirname +  '/../ssl/1_ws.tdreamer.cc_bundle.crt')
   
};

var server = https.createServer(tlsOptions, (app) => {
    console.log((new Date()) + ' Received request for ' + app);
}).listen(443, () => {
    console.log((new Date()) + ' Server is listening on port 443');
});

wsServer = new WebSocketServer({
    server: server
});

function originIsAllowed(origin) {
    // put logic here to detect whether the specified origin is allowed. 
    return true;
}

wsServer.on('connection', function(wsConnect) {
    wsConnect.on('message', function(message) {
        console.log(message);
        wsConnect.send('reply');
    });
});